<?php
/**
 * Created by PhpStorm.
 * User: zyw
 * Date: 2018-06-22
 * Time: 22:09
 */
try{
    set_time_limit(0);
    error_reporting(E_ALL);
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);
    date_default_timezone_set('Asia/Shanghai');
    require_once "../../db/video_db.php";

    $postData = file_get_contents('php://input');
    $data = json_decode($postData, true);

    $uid = isset($data["uid"]) ? $data["uid"] : 0;
    $category_name = isset($data["category_name"]) ? $data["category_name"] : "";
    $category_desc = isset($data["category_desc"]) ? $data["category_desc"] : "";

    $title = isset($data["title"]) ? $data["title"] : "";
    $address = isset($data["address"]) ? $data["address"] : "";
    $time = isset($data["time"]) ? $data["time"] : "";
    $pic = isset($data["pic"]) ? $data["pic"] : ""; //封面图 {"u":"url","w":"宽度","h":"高度","s":"大小"}

    if(empty($category_name) || empty($title) || empty($address) || empty($uid)){
        $res = array("code"=>-1, "result"=>null, "msg"=>"用户ID, 分类名称，配乐标题，配乐地址 不能为空");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = 'select count(*) as num from t_user where uid='.$uid;
    $uNum = video_db::fetch($sql);
    if($uNum["num"] <= 0){
        $res = array("code"=>-1, "result"=>null, "msg"=>"用户不存在");
        echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
    }
    $sql = 'select * from t_bg_category where name="'.$category_name.'" limit 1';
    $category = video_db::fetch($sql);
    if(!empty($category)){
        $category_id = $category["id"];
    }else{
        $category_desc = (string)$category_desc;
        $sql = "insert into t_bg_category(`name`, `desc`, add_time)values('".$category_name."', '".$category_desc."', '".time()."')";
        $category_id = video_db::execute($sql);
        if(empty($category_id)){
            $res = array("code"=>-1, "result"=>null, "msg"=>"添加数据失败");
            echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
        }
    }
    $sql = "select * from t_bg_music where title='".$title."' limit 1";
    $music = video_db::fetch($sql);
    if(!empty($music)){
        $music_id = $music["id"];
    }else{
        $imgId = 0;
        if(!empty($pic["u"])){
            $sql = "insert into t_img(path, w, h, `size`, add_time)values('".$pic["u"]."', '".$pic["w"]."', '".$pic["h"]."', '".$pic["s"]."', '".time()."')";
            $imgId = video_db::execute($sql);
        }
        $sql = "insert into t_bg_music(title, uid, music_address, music_time, pic_id, add_time)values('".$title."','".$uid."','".$address."','".$time."','".$imgId."','".time()."')";
        $music_id = video_db::execute($sql);

        $sql = "insert into t_bg_category_music(category_id, music_id)values(".$category_id.", ".$music_id.")";
        video_db::execute($sql);
    }
    video_db::close();
    $res = array("code"=>0, "result"=>array("music_id" => $music_id), "msg"=>"");
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;

}catch(PDOException $pe){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$pe->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$pe->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}catch (Exception $e){
    video_db::close();
    file_put_contents('../log/error_'.date('Ymd').'.log',date("Y-m-d H:i:s")."  ".$e->getMessage()."\r\n",FILE_APPEND);

    $res = array("code"=>-1, "result"=>null, "msg"=>$e->getMessage());
    echo json_encode($res, JSON_UNESCAPED_UNICODE);exit;
}